$(function () {
    $("#page").load("/back/page.html");
    findAll();
});

function findAll() {
    let data = {};
    data.roomId = $("#roomId").val();
    data.roomType = $("#roomType").val();
    data.startRoomPrice = $("#startRoomPrice").val();
    data.endRoomPrice = $("#endRoomPrice").val();
    data.roomTitle = $("#roomTitle").val();
    data.pageSize = $.trim($("#pageSize").val());
    data.pageNum = $.trim($("#pageNum").val());
    $.ajax({
        url: '/back/room/roomFindAll',
        data: data,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            showRoom(data.list);
            $("#pageCount").html(data.number);
        }
    })
}

function showRoom(data) {
    let html = "";//进行字符串拼接
    for (let i = 0; i < data.length; i++) {
        html += ' <tr>\n' +
            '      <td>' + data[i].roomId + '</td>\n"' +
            '       <td>' + data[i].roomType + '</td>\n' +
            '        <td>' + data[i].roomPrice + '</td>\n' +
            '        <td>' + data[i].roomTitle + '</td>\n' +
            '        <td><img src="' + data[i].roomPhoto + '"> </td>\n' +
            '        <td>' + data[i].roomIntroduce + '</td>\n' +
            '        <td><a  href="javascript:edit( ' + data[i].roomId + ')">编辑</a> <a  href="javascript:del( ' + data[i].roomId + ')">删除</a></td>\n' +
            '    </tr>';
    }
    $("#roomFindAll").html(html)//对当前的tbody进行赋值
}

function edit(roomId) {
    localStorage.setItem("roomId", roomId);
    //sessionStorage.setItem("userId",id);   本地存储  两种方式都可以
    $('#right').load('/back/room/roomEdit.html');//页面跳转
}

function del(roomId) {
    $.ajax({
        url: '/back/room/delete',
        data: {roomId: roomId},
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data === 1) {
              $("#right").load('/back/room/roomList.html');
            } else {
               alert("删除失败")
            }
        }
    })

}
